<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select
						v-model="searchParams.auditState"
						:option-list="auditStateList"
						placeholder="状态"
						clearable
						size="small"
						class="search-bar-item-size1 margin-5-0 item-gap"
					></jk-select>
					<jk-select
						v-model="searchParams.workshopId"
						:option-list="workshopList"
						placeholder="区域"
						size="small"
						class="search-bar-item-size2 margin-5-0 item-gap"
					></jk-select>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" border :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<el-tag v-if="item.keyName === 'typeName'" type="primary" class="width-100-percent">{{ scope.row.typeName }}</el-tag>
						<jk-common-status v-else-if="item.keyName === 'auditState'" :status="scope.row.auditState"></jk-common-status>
						<div
							v-else-if="item.keyName === 'weekList'"
							:shift-list="scope.row.weekList || []"
						>
							<jk-table
								border
								:show-header="false"
								:data="scope.row.weekList || []"
							>
								<vxe-column title="周次" align="center" field="weekName" :width="100">
									<template slot-scope="scope2">
										<span>第{{ scope2.rowIndex + 1 }}周</span>
									</template>
								</vxe-column>
								<vxe-column title="班次" align="left" field="groupList" :min-width="160">
									<template slot-scope="scope2">
										<shiftGroupList
											page-type="detail"
											:shift-list="scope2.row.shiftList || []"
										></shiftGroupList>
									</template>
								</vxe-column>
								<vxe-column title="是否起始" align="center" field="ifDefault" :width="90">
									<template slot-scope="scope2">
										<el-switch
											v-model="scope2.row.ifDefault"
											:disabled="true"
										></el-switch>
									</template>
								</vxe-column>
							</jk-table>
						</div>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template slot-scope="scope">
						<jk-operation-bar
							:scope="scope"
							api-prefix="workforcemanagement/schedule-rule"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<save-dialog
			:page-type="pageType"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import { auditStateList } from '@/utils/jk/status-option-list';
    import saveDialog from './save-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import {
        workforcemanagementShiftPage
    } from '../../../api/schedule/shift';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { workforceManagementScheduleRulePage } from '../../../api/schedule/autoScheduleRule';
    import shiftGroupList from './componens/shift-group-list';

    export default {
        name: 'AutoScheduleRule',
        components: {
            shiftGroupList,
            saveDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                prefix: 'workforcemanagement:scheduleRule',
                apiItem: {
                    'page': workforceManagementScheduleRulePage
                },
                pageType: '',
                auditStateList: auditStateList,
                workshopList: [],
                shiftList: [],
                dataId: null,
                saveDialogState: false,
                tableLoading: false,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    typeName: '',
                    auditState: null
                }
            };
        },
        methods: {
            getShiftListRequest() {
                if (!this.searchParams.workshopId) {
                    this.shiftList = [];
                    return;
                }
                return workforcemanagementShiftPage({
                    workshopId: this.searchParams.workshopId,
                    auditState: 1
                }).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                    }
                });
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.pageType = 'detail';
                this.dataId = e.id;
                this.saveDialogState = true;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.saveDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                this.apiItem.page(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.pageType = 'add';
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.pageType = 'edit';
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.searchParams.workshopId) {
                    // this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
